<template>
  <div class="log-files">
    <el-card class="list-card" shadow="hover">
      <div slot="header" class="card-header">
        <div class="header-left">
          <i class="el-icon-document"></i>
          <span>日志文件</span>
          <el-tooltip content="查看和下载数据库日志文件" placement="right">
            <i class="el-icon-question"></i>
          </el-tooltip>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="search-bar">
        <el-form :inline="true" :model="searchForm" ref="searchForm" size="small">
          <el-form-item label="日期范围" prop="dateRange" required>
            <el-date-picker
              v-model="searchForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              style="width: 340px">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch" :disabled="!searchForm.dateRange">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 日志文件列表 -->
      <el-table
        :data="tableData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        stripe>
        <el-table-column prop="startTime" label="开始时间" min-width="180"></el-table-column>
        <el-table-column prop="endTime" label="结束时间" min-width="180"></el-table-column>
        <el-table-column prop="fileSize" label="文件大小" width="120">
          <template slot-scope="scope">
            <span class="file-size">{{ scope.row.fileSize }}</span>
          </template>
        </el-table-column>
        <el-table-column label="下载地址" width="100" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="handleDownload(scope.row)">
              下载
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 空状态 -->
      <div v-if="!loading && tableData.length === 0" class="empty-state">
        <img src="/placeholder.svg?height=120&width=120" alt="No Data" />
        <p>暂无日志文件</p>
      </div>

      <!-- 分页器 -->
      <div v-if="tableData.length > 0" class="pagination-container">
        <span class="total-count">共 {{ total }} 条数据</span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>

    <!-- 下载进度对话框 -->
    <el-dialog
      title="下载进度"
      :visible.sync="downloadDialogVisible"
      :close-on-click-modal="false"
      :show-close="false"
      width="400px">
      <div class="download-progress">
        <el-progress
          :percentage="downloadProgress"
          :status="downloadProgress === 100 ? 'success' : ''"
          :stroke-width="18">
        </el-progress>
        <p class="progress-text">{{ downloadProgress === 100 ? '下载完成' : '正在下载日志文件...' }}</p>
      </div>
      <div slot="footer" class="dialog-footer" v-if="downloadProgress === 100">
        <el-button type="primary" @click="downloadDialogVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'LogFiles',
  data() {
    return {
      // 搜索相关
      searchForm: {
        dateRange: []
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      loading: false,

      // 表格数据
      tableData: [
        {
          startTime: '2024-11-25 23:55:55',
          endTime: '2024-11-25 23:56:48',
          fileSize: '1.88M'
        }
      ],

      // 分页相关
      currentPage: 1,
      pageSize: 10,
      total: 1,

      // 下载相关
      downloadDialogVisible: false,
      downloadProgress: 0,
      downloadTimer: null
    }
  },
  methods: {
    // 搜索相关方法
    handleSearch() {
      if (!this.searchForm.dateRange || this.searchForm.dateRange.length !== 2) {
        this.$message.warning('请选择日期范围')
        return
      }
      this.currentPage = 1
      this.getList()
    },
    resetSearch() {
      this.$refs.searchForm.resetFields()
      this.tableData = []
      this.total = 0
    },

    // 分页相关方法
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 下载相关方法
    handleDownload(row) {
      this.downloadDialogVisible = true
      this.downloadProgress = 0

      // 模拟下载进度
      this.downloadTimer = setInterval(() => {
        if (this.downloadProgress < 100) {
          this.downloadProgress += 10
        } else {
          clearInterval(this.downloadTimer)
          // 模拟文件下载
          setTimeout(() => {
            this.$message.success('日志文件下载成功')
          }, 500)
        }
      }, 300)
    },

    // 获取列表数据
    getList() {
      this.loading = true
      // 模拟API调用
      setTimeout(() => {
        this.loading = false
      }, 800)
    }
  },
  beforeDestroy() {
    if (this.downloadTimer) {
      clearInterval(this.downloadTimer)
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style scoped>
.log-files {
  padding: 0px;
}

.list-card {
  margin-bottom: 20px;
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-left i {
  font-size: 18px;
}

.header-left .el-icon-question {
  font-size: 14px;
  color: #909399;
  cursor: help;
}

.search-bar {
  margin-bottom: 20px;
  padding: 18px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.file-size {
  font-family: monospace;
  color: #67C23A;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
  color: #909399;
}

.empty-state img {
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-state p {
  margin: 0;
  font-size: 14px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.total-count {
  color: #606266;
  font-size: 14px;
}

/* 表格样式优化 */
.el-table {
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 20px;
}

.el-table th {
  background-color: #f5f7fa !important;
  color: #606266;
  font-weight: bold;
}

.el-table--striped .el-table__body tr.el-table__row--striped td {
  background-color: #fafafa;
}

/* 表单样式优化 */
.el-form--inline .el-form-item {
  margin-right: 20px;
}

.el-form--inline .el-form-item:last-child {
  margin-right: 0;
}

/* 日期选择器样式 */
.el-date-editor .el-range-separator {
  padding: 0 5px;
}

.el-date-editor--daterange.el-input__inner {
  width: 340px;
}

/* 下载进度对话框样式 */
.download-progress {
  padding: 20px;
  text-align: center;
}

.progress-text {
  margin-top: 15px;
  color: #606266;
  font-size: 14px;
}

/* 加载动画样式 */
.el-loading-spinner .el-icon-loading {
  font-size: 30px;
  color: #409EFF;
}

.el-loading-spinner .el-loading-text {
  color: #409EFF;
  margin: 3px 0;
  font-size: 14px;
}
</style>
